<template>
  <div class="main">
    <el-card class="box-card">
      <Form ref="form" :formobj="formobj" :formdata="formData" :formrules="formrules" />
      <div style="padding-left:100px">
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-card>
  </div>
</template>
<script>
import Form from '@/components/form.vue'
import { updateFirstSwich, getFirstSwich, getConfig, setConfig } from '@/api/webviews'
export default {
  name: 'Control',
  components: {
    Form
  },
  data() {
    return {
      formData: {
        swich: '',
        isFilter: ''
      },
      formobj: [
        { label: '弹出方式', key: 'swich', type: 'radio', options: [
          { label: '初次', value: '0' },
          { label: '总是', value: '1' }
        ]
        },
        { label: '默哀模式', key: 'isFilter', type: 'radio', options: [
          { label: '是', value: '0' },
          { label: '否', value: '1' }
        ]
        }
      ],
      formrules: {
        swich: [
          { required: true, message: '请选择', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getdata()
  },
  methods: {
    getdata() {
      getConfig({ key: 'isFilter' }).then(res => {
        console.log(res)
        if (res.code === 0) {
          this.formData.isFilter = res.data ? res.data : '1'
        }
      })
      getFirstSwich().then(res => {
        if (res.code == 0) {
          this.formData.swich = res.data
          console.log('🚀 ~ file: index.vue ~ line 44 ~ contentApiqueryOne ~ code', code)
        }
      })
    },
    submit() {
      this.$refs.form.getData().then(res => {
        if (res.isFilter) {
          setConfig({ key: 'isFilter', value: res.isFilter }).then(res => {
            this.getdata()
          })
        }

        updateFirstSwich(res).then((response) => {
          if (response.data == 1) {
            this.$message.success('成功')
          } else {
            this.$message.error('失败')
          }
        })
          .catch((err) => {
            console.log(err)
          })
      })
    }
  }
}
</script>
<style scoped>
@import "~@/assets/css/adminstyle.css";
</style>
